﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class CustomerDAO
    {
        DataServices m_CustomerDAO = new DataServices();

        public DataTable GetAll()
        {
            m_CustomerDAO.Load("GetAllCustomer", null);
            return m_CustomerDAO;
        }

        public DataTable GetById(String cusId)
        {
            SqlParameter param = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param.Value = cusId;

            m_CustomerDAO.Load("GetCustomerById", param);
            return m_CustomerDAO;
        }

        public DataTable GetByLikeId(String cusId)
        {
            SqlParameter param = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param.Value = cusId;

            m_CustomerDAO.Load("GetCustomerByLikeId", param);
            return m_CustomerDAO;
        }

        public DataTable GetByName(String fullName)
        {
            SqlParameter param = new SqlParameter("@FullName", SqlDbType.NVarChar, 100);
            param.Value = fullName;

            m_CustomerDAO.Load("GetCustomerByName", param);
            return m_CustomerDAO;
        }

        public DataTable GetByDiscount(double min, double max)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@DiscountMin", SqlDbType.Money);
            param[0].Value = min;

            param[1] = new SqlParameter("@DiscountMax", SqlDbType.Money);
            param[1].Value = max;

            m_CustomerDAO.Load("GetCustomerByDiscount", param);
            return m_CustomerDAO;
        }

        public DataTable GetByAddress(String address)
        {
            SqlParameter param = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param.Value = address;

            m_CustomerDAO.Load("GetCustomerByAddress", param);
            return m_CustomerDAO;
        }

        public DataTable GetByPhone(String phone)
        {
            SqlParameter param = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param.Value = phone;

            m_CustomerDAO.Load("GetCustomerByPhone", param);
            return m_CustomerDAO;
        }

        public DataTable GetByEmail(String email)
        {
            SqlParameter param = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param.Value = email;

            m_CustomerDAO.Load("GetCustomerByEmail", param);
            return m_CustomerDAO;
        }

        public DataTable GetForReport()
        {
            m_CustomerDAO.Load("GetCustomerReport", null);
            return m_CustomerDAO;
        }

        public int Add(CustomerInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param[0].Value = info.CusId;

            param[1] = new SqlParameter("@FullName", SqlDbType.NVarChar, 100);
            param[1].Value = info.FullName;

            param[2] = new SqlParameter("@Sex", SqlDbType.Bit);
            param[2].Value = info.Sex;

            param[3] = new SqlParameter("@DiscountPer", SqlDbType.Money);
            param[3].Value = info.DiscountPer;

            param[4] = new SqlParameter("@Debt", SqlDbType.Money);
            param[4].Value = info.Debt;

            param[5] = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param[5].Value = info.Address;

            param[6] = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param[6].Value = info.Phone;

            param[7] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param[7].Value = info.Email;

            param[8] = new SqlParameter("@State", SqlDbType.NVarChar, 100);
            param[8].Value = info.State;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return m_CustomerDAO.ExecuteUpdate("AddCustomer", param);
        }

        public int Update(CustomerInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param[0].Value = info.CusId;

            param[1] = new SqlParameter("@FullName", SqlDbType.NVarChar, 100);
            param[1].Value = info.FullName;

            param[2] = new SqlParameter("@Sex", SqlDbType.Bit);
            param[2].Value = info.Sex;

            param[3] = new SqlParameter("@DiscountPer", SqlDbType.Money);
            param[3].Value = info.DiscountPer;

            param[4] = new SqlParameter("@Debt", SqlDbType.Money);
            param[4].Value = info.Debt;

            param[5] = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param[5].Value = info.Address;

            param[6] = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param[6].Value = info.Phone;

            param[7] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param[7].Value = info.Email;

            param[8] = new SqlParameter("@State", SqlDbType.NVarChar, 100);
            param[8].Value = info.State;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return m_CustomerDAO.ExecuteUpdate("UpdateCustomer", param);
        }

        public int UpdateSex(String cusId)
        {
            SqlParameter param = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param.Value = cusId;

            return m_CustomerDAO.ExecuteUpdate("UpdateCustomerSex", param);
        }

        public int Delete(String cusId)
        {
            SqlParameter param = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param.Value = cusId;

            return m_CustomerDAO.ExecuteUpdate("DeleteCustomer", param);
        }

        public DataRow AddNewRow()
        {
            return m_CustomerDAO.NewRow();
        }

        public void AddNewCustomer(DataRow m_Row)
        {
            m_CustomerDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_CustomerDAO.ExecuteNoneQuery() > 0;
        }
    }
}
